import Toast from 'tdesign-miniprogram/toast/index';
import {
    resolveAddress,
    rejectAddress
} from './util';

import {
    createAddress,
    areaTree,
    updateAddress,
    getAddress,
    defaulAddress
} from '../../../../api/address';

const innerPhoneReg = '^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[0-8]|8\\d|9\\d)\\d{8}$';
const innerNameReg = '^[a-zA-Z\\d\\u4e00-\\u9fa5]+$';

Page({
    options: {
        multipleSlots: true,
    },
    externalClasses: ['theme-wrapper-class'],
    data: {
        locationState: {
            labelIndex: null,
            addressId: '',
            addressTag: '',
            cityCode: '',
            cityName: '',
            countryCode: '',
            countryName: '',
            detailAddress: '',
            districtCode: '',
            districtName: '',
            isDefault: false,
            name: '',
            phone: '',
            provinceCode: '',
            provinceName: '',
            isEdit: false,
            isOrderDetail: false,
            isOrderSure: false,
        },
        areaData: [],
        areaPickerVisible: false,
        submitActive: false,
        visible: false,
        labelValue: '',
        columns: 3,
        verifyTips: '',
        id: '',
        areaName: '',
        disabled: false
    },
    onLoad(options) {
        const {
            id
        } = options;
        this.setData({
            id: id || ''
        })
        this.init(id);
    },

    onUnload() {
        if (!this.hasSava) {
            rejectAddress();
        }
    },

    hasSava: false,

    async init(id) {
        if (id) {
            this.getAddressDetail(Number(id));
        } else {
            const ret = await defaulAddress();
            this.setData({
                'locationState.isDefault': ret ? false : true,
                disabled: ret ? false : true
            })
        }
    },
    async getAddressDetail(id) {
        const res = await getAddress(id)
        this.setData({
            'locationState.name': res.name,
            'locationState.phone': res.mobile,
            'locationState.districtCode': res.areaId,
            'locationState.detailAddress': res.detailAddress,
            'locationState.isDefault': res.defaultStatus,
            areaName: res.areaName,
            disabled: res.defaultStatus
        }, () => {
            const {
                isLegal,
                tips
            } = this.onVerifyInputLegal();
            this.setData({
                submitActive: isLegal,
                verifyTips: tips
            });
        });
    },
    onInputValue(e) {
        const {
            item
        } = e.currentTarget.dataset;

        if (item === 'address') {
            const {
                selectedOptions = []
            } = e.detail;
            this.setData({
                    'locationState.provinceCode': selectedOptions[0].value,
                    'locationState.provinceName': selectedOptions[0].label,
                    'locationState.cityName': selectedOptions[1].label,
                    'locationState.cityCode': selectedOptions[1].value,
                    'locationState.districtCode': selectedOptions[2].value,
                    'locationState.districtName': selectedOptions[2].label,
                    areaPickerVisible: false,
                    areaName: selectedOptions[0].label + '-' + selectedOptions[1].label + '-' + selectedOptions[2].label
                },
                () => {
                    const {
                        isLegal,
                        tips
                    } = this.onVerifyInputLegal();
                    this.setData({
                        submitActive: isLegal,
                        verifyTips: tips
                    });
                },
            );
        } else {
            const {
                value = ''
            } = e.detail;
            this.setData({
                    [`locationState.${item}`]: value,
                },
                () => {
                    const {
                        isLegal,
                        tips
                    } = this.onVerifyInputLegal();
                    this.setData({
                        submitActive: isLegal,
                        verifyTips: tips
                    });
                },
            );
        }
    },
    async onPickArea() {
        const res = await areaTree();
        this.setData({
            areaData: res,
            areaPickerVisible: true
        })
    },
    onPickLabels(e) {
        const {
            item
        } = e.currentTarget.dataset;
        const {
            locationState: {
                labelIndex = undefined
            },
            labels = [],
        } = this.data;
        let payload = {
            labelIndex: item,
            addressTag: labels[item].name,
        };
        if (item === labelIndex) {
            payload = {
                labelIndex: null,
                addressTag: ''
            };
        }
        this.setData({
            'locationState.labelIndex': payload.labelIndex,
        });
        this.triggerEvent('triggerUpdateValue', payload);
    },
    addLabels() {
        this.setData({
            visible: true,
        });
    },
    confirmHandle() {
        const {
            labels,
            labelValue
        } = this.data;
        this.setData({
            visible: false,
            labels: [...labels, {
                id: labels[labels.length - 1].id + 1,
                name: labelValue
            }],
            labelValue: '',
        });
    },
    cancelHandle() {
        this.setData({
            visible: false,
            labelValue: '',
        });
    },
    onCheckDefaultAddress({
        detail
    }) {
        const {
            value
        } = detail;
        this.setData({
            'locationState.isDefault': value,
        });
    },

    onVerifyInputLegal() {
        const {
            name,
            phone,
            detailAddress,
            districtName
        } = this.data.locationState;
        const prefixPhoneReg = String(this.properties.phoneReg || innerPhoneReg);
        const prefixNameReg = String(this.properties.nameReg || innerNameReg);
        const nameRegExp = new RegExp(prefixNameReg);
        const phoneRegExp = new RegExp(prefixPhoneReg);

        if (!name || !name.trim()) {
            return {
                isLegal: false,
                tips: '请填写收货人',
            };
        }
        if (!nameRegExp.test(name)) {
            return {
                isLegal: false,
                tips: '收货人仅支持输入中文、英文（区分大小写）、数字',
            };
        }
        if (!phone || !phone.trim()) {
            return {
                isLegal: false,
                tips: '请填写手机号',
            };
        }
        if (!phoneRegExp.test(phone)) {
            return {
                isLegal: false,
                tips: '请填写正确的手机号',
            };
        }
        if (!this.data.areaName || !this.data.areaName.trim()) {
            return {
                isLegal: false,
                tips: '请选择省市区信息',
            };
        }
        if (!detailAddress || !detailAddress.trim()) {
            return {
                isLegal: false,
                tips: '请完善详细地址',
            };
        }
        if (detailAddress && detailAddress.trim().length > 50) {
            return {
                isLegal: false,
                tips: '详细地址不能超过50个字符',
            };
        }
        return {
            isLegal: true,
            tips: '添加成功',
        };
    },

    builtInSearch({
        code,
        name
    }) {
        return new Promise((resolve, reject) => {
            wx.getSetting({
                success: (res) => {
                    if (res.authSetting[code] === false) {
                        wx.showModal({
                            title: `获取${name}失败`,
                            content: `获取${name}失败，请在【右上角】-小程序【设置】项中，将【${name}】开启。`,
                            confirmText: '去设置',
                            confirmColor: '#FA550F',
                            cancelColor: '取消',
                            success(res) {
                                if (res.confirm) {
                                    wx.openSetting({
                                        success(settinRes) {
                                            if (settinRes.authSetting[code] === true) {
                                                resolve();
                                            } else {
                                                console.warn('用户未打开权限', name, code);
                                                reject();
                                            }
                                        },
                                    });
                                } else {
                                    reject();
                                }
                            },
                            fail() {
                                reject();
                            },
                        });
                    } else {
                        resolve();
                    }
                },
                fail() {
                    reject();
                },
            });
        });
    },

    onSearchAddress() {
        let that = this
        this.builtInSearch({
            code: 'scope.userLocation',
            name: '地址位置'
        }).then(() => {
            wx.chooseLocation({
                success: (res) => {
                    if (res.name) {
                        const {
                            isLegal,
                            tips
                        } = that.onVerifyInputLegal();
                        that.setData({
                            ['locationState.detailAddress']: res.address,
                            submitActive: isLegal,
                            verifyTips: tips
                        });
                    } else {
                        Toast({
                            context: this,
                            selector: '#t-toast',
                            message: '地点为空，请重新选择',
                            icon: '',
                            duration: 1000,
                        });
                    }
                },
                fail: function (res) {
                    console.warn(`wx.chooseLocation fail: ${JSON.stringify(res)}`);
                    if (res.errMsg !== 'chooseLocation:fail cancel') {
                        Toast({
                            context: this,
                            selector: '#t-toast',
                            message: '地点错误，请重新选择',
                            icon: '',
                            duration: 1000,
                        });
                    }
                },
            });
        });
    },
    async formSubmit() {
        const {
            submitActive
        } = this.data;
        if (!submitActive) {
            Toast({
                context: this,
                selector: '#t-toast',
                message: this.data.verifyTips,
                icon: '',
                duration: 1000,
            });
            return;
        }
        const {
            locationState
        } = this.data;

        this.hasSava = true;
        let params = {
            name: locationState.name,
            mobile: locationState.phone,
            areaId: locationState.districtCode,
            detailAddress: locationState.detailAddress,
            defaultStatus: locationState.isDefault,
        }
        if (this.data.id) {
            params.id = this.data.id
            const res = await updateAddress(params);
        } else {
            const res = await createAddress(params);
        }
        wx.navigateBack();
    },

    getWeixinAddress(e) {
        const {
            locationState
        } = this.data;
        const weixinAddress = e.detail;
        this.setData({
                locationState: {
                    ...locationState,
                    ...weixinAddress
                },
            },
            () => {
                const {
                    isLegal,
                    tips
                } = this.onVerifyInputLegal();
                this.setData({
                    submitActive: isLegal,
                    verifyTips: tips
                });
            },
        );
    },
});